package com.android.driveuploader;

/**
 * Created by srikanthg on 8/27/14.
 */
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;

import java.util.List;

/**
 * Activity that allows users to select the meme template.
 */
public class SelectMemeActivity extends ActionBarActivity {

    /**
     * Fragment that displays the meme.
     */
    public static class SelectMemeFragment extends Fragment {
        @Override
        public View onCreateView(
                LayoutInflater inflater,
                ViewGroup container,
                Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            return inflater.inflate(R.layout.fragment_select_meme, container, false);
        }
    }

    private static final String LOG_TAG = SelectMemeActivity.class.getCanonicalName();
    private static final String MEME_FEED_URL = "https://picasaweb.google.com/"
            + "data/feed/base/user/"
            + "116526417878498972096/albumid/5750911813501865489?kind=photo"
            + "&authkey=Gv1sRgCIHapMbBmI-VuAE&alt=json"
            + "&fields=entry(media:group(media:content,media:thumbnail))";

    private ImageAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_meme);
        GridView gridView = (GridView) findViewById(R.id.gridview);
        adapter = new ImageAdapter(this, VolleyUtils.getImageLoader(this));
        gridView.setAdapter(adapter);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                Intent intent = CreateMemeActivity.createIntent(
                        SelectMemeActivity.this,
                        (String) adapter.getItem(position));
                startActivity(intent);
            }
        });

        ImageUrlFetcher imageUrlFetcher = new ImageUrlFetcher(this);
        imageUrlFetcher.fetch(MEME_FEED_URL, new ImageUrlFetcher.Callback() {
            @Override
            public void onResult(List<String> urls) {
                adapter.setImageUrls(urls);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Exception e) {
                Log.e(LOG_TAG, "Error downloading the image urls", e);
                Toast.makeText(
                        SelectMemeActivity.this,
                        getString(R.string.error_downloading_image_urls),
                        Toast.LENGTH_LONG)
                        .show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.select_meme, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onDestroy() {
        // Cancel any in-flight requests.
        // Note that this will cancel requests upon rotation as well, which could be avoided if
        // desired by using a loader or a retained fragment to make the request.
        VolleyUtils.getRequestQueue(this).cancelAll(this);
        super.onDestroy();
    }
}
